package com.cndw.kungfu.model.player;

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;
import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.config.ConstantsSystem;
import com.cndw.kungfu.dao.LogRegDao;
import com.cndw.kungfu.dao.PlayerDao;
import com.cndw.kungfu.domain.DataInfoPlayer;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.Vocation;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.SerializeProcesser;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;

/**
 * 角色信息操作
 * 
 * @author fantadust
 * 
 */
@Component
public class PlayerManager {

	@Autowired
	private PlayerDao dao;
	@Autowired
	private LogRegDao logDao;
	
	private static long currentAutoNumber = -1;

	private synchronized long getNextPlayerId() {

		if (currentAutoNumber < 0) {
			long currentPlayerId = 0;
			try {
				currentPlayerId = dao.getMaxPlayerId();
			} catch (NullPointerException e) {

			} catch (Exception e) {
				e.printStackTrace();
			}
			currentAutoNumber = (currentPlayerId / 1000);
		}
		currentAutoNumber++;

		return currentAutoNumber * 1000 + ConstantsConfig.getSysCgAutoNumberSuffix();
	}

	/**
	 * 插入一个新角色
	 * 
	 * @param accountName
	 * @param playerName
	 * @param vocationId
	 * @param lastIp
	 * @return
	 */
	public Player insert(String accountName, String playerName, int vocationId, String lastIp,String platform) {

		Vocation vocation = Context.getCongfigServer().getVocation(vocationId);

		if (vocation == null)
			return null;
		Player player = new Player();

		player.setPlayerId(getNextPlayerId());

		player.setPlayerName(playerName);
		player.setAccountName(accountName);
		player.setVocationId(vocationId);
		player.setSex(vocation.getSex());
		player.setLevel(1);// 默认一级

		player.setPower(vocation.getPower());
		player.setPhysical(vocation.getPhysical());
		player.setAgile(vocation.getAgile());
		player.setWit(vocation.getWit());

		player.setBoxsGirds(36);
		player.setHoldGrids(45);
		
		Context.getPlayerServer().initProperties(player);

		player.setHp(player.getMaxHp());
		player.setMp(player.getMaxMp());

		player.setGold(0);
		player.setGoldTicke(0);
		player.setAsset(1000);

		player.setLastIp(lastIp);
		player.setLastLogin(TimeProcesser.getUnixTime());
		player.setRegTime(TimeProcesser.getUnixTime());
		
		player.setPlatform(platform);
		
		try {// 插入数据库,并设置用户id
			dao.insert(player);
			return player;
		}catch (DuplicateKeyException e) {
			LogUtil.getMain().error(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;

	}

	/**
	 * 以用户帐号获取用户id
	 * 
	 * @param accountName
	 * @return
	 */
	public long getPlayerIdByAccountName(String accountName) {
		try {
			return dao.getByAccountName(accountName);
		} catch (Exception e) {
			return 0;
		}

	}

	/**
	 * 以用户角色名称获得用户id
	 * 
	 * @param playerName
	 * @return
	 */
	public long getPlayerIdByPlayerName(String playerName) {
		try {
			return dao.getByPlayerName(playerName);
		} catch (Exception e) {
			return 0;
		}
	}

	/**
	 * 从数据库拿用户对象
	 * 
	 * @param playerId
	 * @return
	 */
	public Player getByPlayerIdFromDb(long playerId) {
		try {
			return dao.getByPlayerId(playerId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Map<Integer, Integer> getCountsGroupDB() {
		try {
			return dao.getCountsGroup();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public long getRatePlayerId(){
		try {
			return dao.getRatePlayerId();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 把用户对象同步到数据库
	 * 
	 * @param player
	 */
	public boolean saveToDb(Player player) {
		if (player == null || player.getPlayerId() <= 0) {
			return true;
		}
		try {
			dao.update(player);
		} catch (Exception e) {
			ConstantsSystem.throwExceptionDB(e);
			return false;
		}
		return true;
	}

	/**
	 * 把用户临时数据同步到db
	 * 
	 * @param playerId
	 * @param cacheData
	 */
	public boolean saveTempData(PlayerDataInfo playerDataInfo, String cacheData) {
		int syncDBTime = TimeProcesser.getUnixTime();
		try {

			if (playerDataInfo.getSyncDBTime() > 0) {
				dao.syncTempData(playerDataInfo.getPlayerId(), cacheData, syncDBTime);
			} else {
				dao.insertTempData(playerDataInfo.getPlayerId(), cacheData, syncDBTime);
			}
			playerDataInfo.setSyncDBTime(syncDBTime);
		} catch (DuplicateKeyException e2) {
			try {
				dao.syncTempData(playerDataInfo.getPlayerId(), cacheData, syncDBTime);
				playerDataInfo.setSyncDBTime(syncDBTime);
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}

		} catch (Exception e) {
			ConstantsSystem.throwExceptionDB(e);
			return false;
		}
		return true;
	}

	public PlayerDataInfo loadDataInfo(long playerId) {
		try {
			DataInfoPlayer dataInfoPlayer = dao.getDataInfo(playerId);
			if (dataInfoPlayer != null) {
				PlayerDataInfo playerDataInfo = null;
				try {
					playerDataInfo = (PlayerDataInfo) SerializeProcesser.readObject(dataInfoPlayer.getCacheData());

				} catch (Exception e) {
					e.printStackTrace();
				}
				if (playerDataInfo == null) {
					LogUtil.getMain().error("err datainfo id "+playerId);
					playerDataInfo = new PlayerDataInfo(playerId);
				}
				playerDataInfo.setSyncDBTime(dataInfoPlayer.getSyncDBTime());
				return playerDataInfo;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return new PlayerDataInfo(playerId);
	}

	public boolean registerPlayerIdCard(long playerId, String name, String idcard) {
		try {
			dao.insertPlayerIdCard(playerId, name, idcard);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean logRegInsert(String accountName,int timeFirst){
		try {
			logDao.insert(accountName, timeFirst);			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public boolean logRegUpdate(String accountName,int state){
		try {
			logDao.update(accountName, state);			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
}
